package cn.cai.pethome.system.listener;

import cn.cai.pethome.base.PreAuthorize;
import cn.cai.pethome.base.util.Classutil;
import cn.cai.pethome.system.domain.Permission;
import cn.cai.pethome.system.mapper.PermissionMapper;
import cn.cai.pethome.system.service.IPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@WebListener
public class PermissionScanInitListener implements ServletContextListener {

    @Autowired
    private IPermissionService permissionService;

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        //开启一个分支线程：采用异步编程去执行扫描，这样不影响主线程（启动项目）的效率
        new Thread(new Runnable() {
            @Override
            public void run() {
                permissionService.scanPermission();
            }
        }).start();


    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
      //项目关闭的时候，我们什么事情都不用干
        log.info("服务器正在关闭.....................");
    }
}
